<template>
  <Modal
    v-model='isShow'
    title='添加环境'
    @on-ok='confirm'
    :loading='isLoading'
    @on-cancel='cancel'>
    <Input v-model='name' placeholder='请输入环境名称' />
  </Modal>
</template>

<script>
import axios from 'axios'
import { HTTP_API } from '@/global/global'

export default {
  name: 'addEnvDialog.vue',
  data() {
    return {
      isShow: false,
      isFetch: false,
      isLoading: true,
      name: null
    }
  },
  methods: {
    show() {
      this.isShow = true
      this.name = null
    },
    close() {
      this.isShow = false
    },
    async confirm() {
      if (this.name == null || this.name == '') {
        this.$Message.warning('请输入名称')
        this.isLoading = false
        this.$nextTick(() => {
          this.isLoading = true
        })
        return
      }
      try {
        const formData = new FormData()
        formData.append('name', this.name)
        await axios.post(HTTP_API + '/env/add', formData)
        this.$Message.success('新增成功')
      } catch (err) {
        this.$Message.error('新增失败')
      } finally {
        this.isShow = false
        this.$emit('refresh')
      }
    },
    cancel() {
      this.isShow = false
    }
  }
}
</script>

<style scoped>

</style>